package com.tedu.bill.demo5;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Author：毕龙
 * Date: 2024/5/24 15:14
 */
public class Train1 extends Thread {
    private static int ticket;//火车票

    //创建锁对象
    static Lock lock = new ReentrantLock();

    @Override
    public void run() {
        while (true) {
            //获取锁标记
            lock.lock();
            //当票卖了120张时 退出循环
            if (ticket == 120) {
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            ticket++;

            System.out.println(Thread.currentThread().getName() + "第" + ticket + "张票卖出");
            //释放锁
            lock.unlock();
        }
    }

}
